#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Key.h"
#include "Timer.h"

uint16_t ArrayWrite[] = {0x1234, 0x5678};
uint16_t ArrayRead[2];
uint8_t KeyNum;

uint8_t flag; // 展示当前按下的是哪一个按键
int main(void)
{
	/*模块初始化*/
	OLED_Init();		
	Key_Init();
    Timer_Init();
	
    OLED_ShowString(1,4,"DR1   DR2");
    OLED_ShowString(2,1,"W:");
    OLED_ShowString(3,1,"R:");
    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
    
    PWR_BackupAccessCmd(ENABLE);

    
    

	while (1)
	{
		KeyNum = Key_GetNum();
        
        if (KeyNum == 1)flag = 1;
        else if (KeyNum == 2)flag = 2;
        OLED_ShowString(4, 1, "KeyNum:");
        OLED_ShowNum(4, 8, flag, 1);  // 显示一下当前按的是哪一个按键
        
        if (KeyNum == 1)
        {
            ArrayWrite[0]++;
            ArrayWrite[1]++;
            
            BKP_WriteBackupRegister(BKP_DR1, ArrayWrite[0]);
            BKP_WriteBackupRegister(BKP_DR2, ArrayWrite[1]);
            
            OLED_ShowHexNum(2, 4, ArrayWrite[0], 4);
            OLED_ShowHexNum(2, 10, ArrayWrite[1], 4);
        }
        if (KeyNum == 2)
        {
            ArrayWrite[0]--;
            ArrayWrite[1]--;
            
            BKP_WriteBackupRegister(BKP_DR1, ArrayWrite[0]);
            BKP_WriteBackupRegister(BKP_DR2, ArrayWrite[1]);
            
            OLED_ShowHexNum(2, 4, ArrayWrite[0], 4);
            OLED_ShowHexNum(2, 10, ArrayWrite[1], 4);
        }
        OLED_ShowHexNum(3, 4, BKP_ReadBackupRegister(BKP_DR1), 4);
        OLED_ShowHexNum(3, 10, BKP_ReadBackupRegister(BKP_DR2), 4);
	}
}


void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		Key_Tick();
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
}
